package com.joesem.sale.client.logic.workOrder.details

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.FragmentOrderDetailsContentBinding
import com.jiaxing.sale.client.databinding.FragmentOrderReturmReceiptBinding
import com.jiaxing.sale.client.databinding.ItemHeaderOrderDetailsRetureBinding
import com.jiaxing.sale.client.databinding.ItemImgBinding
import com.joesem.sale.client.base.BaseFragment
import com.joesem.sale.client.base.gone
import com.joesem.sale.client.base.visible
import com.joesem.sale.client.network.data.OrderDetailsMode
import com.joesem.sale.client.network.data.ReceiptMode
import com.joesem.sale.client.utils.click
import com.joesem.sale.client.widget.dialog.imagePreview

class OrderReturnReceiptFragment : BaseFragment<FragmentOrderReturmReceiptBinding>() {
    private val serviceContentAdapter =
        object :
            BaseQuickAdapter<ReceiptMode.ServiceMode, BaseViewHolder>(R.layout.item_order_details_return) {
            override fun convert(holder: BaseViewHolder, item: ReceiptMode.ServiceMode) {
                holder.setText(R.id.tvContent, item.name.orEmpty())
                holder.setText(R.id.tvNum, item.count.toString())
                holder.setText(R.id.tvUnit, item.unit.orEmpty())
                holder.setText(R.id.tvPrice, item.fee_format.toString())
            }
        }

    override fun FragmentOrderReturmReceiptBinding.initView(savedInstanceState: Bundle?) {
        rvServiceContent.adapter = serviceContentAdapter
    }

    fun refresh(data: OrderDetailsMode?) {
        bind.apply {
            data?.let { it ->
                Glide.with(requireContext())
                    .load(it.receipt?.signature_url)
                    .into(ivSignature)
                ivSignature.click { v ->
                    imagePreview(
                        requireContext(),
                        childFragmentManager,
                        listOf(it.receipt?.signature_url.orEmpty()),
                    )
                }
                tvReason.text = it.receipt?.reason.orEmpty()
                tvProcessType.text = it.receipt?.process_type.orEmpty()
                tvDescription.text = it.receipt?.description.orEmpty()
                it.receipt?.services?.filterNotNull()?.let {
                    if (it.isNotEmpty()) {
                        serviceContentAdapter.removeAllHeaderView()
                        serviceContentAdapter.addHeaderView(
                            ItemHeaderOrderDetailsRetureBinding.inflate(
                                LayoutInflater.from(requireContext())
                            ).root
                        )
                        serviceContentAdapter.setList(it)
                    }
                }
                rbRating1.rating = (it.receipt?.satisfaction ?: 0).toFloat()
                when (rbRating1.rating) {
                    1f -> "非常差"
                    2f -> "差"
                    3f -> "一般"
                    4f -> "好"
                    5f -> "非常好"
                    else -> ""
                }.apply {
                    tvRating1.text = this
                }
                rbRating2.rating = (it.receipt?.attitude ?: 0).toFloat()
                when (rbRating2.rating) {
                    1f -> "极差"
                    2f -> "一般"
                    3f -> "良好"
                    4f -> "满意"
                    5f -> "非常满意"
                    else -> ""
                }.apply {
                    tvRating2.text = this
                }
                rbRating3.rating = (it.receipt?.efficiency ?: 0).toFloat()
                when (rbRating3.rating) {
                    1f -> "很慢"
                    2f -> "慢"
                    3f -> "一般"
                    4f -> "快"
                    5f -> "非常快"
                    else -> ""
                }.apply {
                    tvRating3.text = this
                }
                if (!it.images.isNullOrEmpty()) {
                    tvServiceFrontImg.visible()
                    llServiceFrontImg.visible()
                    llServiceFrontImg.removeAllViews()
                    it.images.forEachIndexed { index, image ->
                        val bindItem = ItemImgBinding.inflate(LayoutInflater.from(requireContext()))
                        bindItem.btnImgDelete.gone()
                        bindItem.root.click { v ->
                            imagePreview(
                                requireContext(),
                                childFragmentManager,
                                it.images.map { it?.img_url.orEmpty() },
                                index
                            )
                        }
                        Glide.with(requireContext())
                            .load(image?.img_url)
                            .into(bindItem.btnImg)
                        llServiceFrontImg.addView(bindItem.root)
                    }
                }
                if (!it.receipt?.images.isNullOrEmpty()) {
                    tvServiceAfterImg.visible()
                    llServiceAfterImg.visible()
                    llServiceAfterImg.removeAllViews()
                    it.receipt?.images?.forEachIndexed { index, image ->
                        val bindItem = ItemImgBinding.inflate(LayoutInflater.from(requireContext()))
                        bindItem.btnImgDelete.gone()
                        bindItem.root.click { v ->
                            imagePreview(
                                requireContext(),
                                childFragmentManager,
                                it.receipt.images.map { it?.image_url.orEmpty() },
                                index
                            )
                        }
                        Glide.with(requireContext())
                            .load(image?.image_url)
                            .into(bindItem.btnImg)
                        llServiceAfterImg.addView(bindItem.root)
                    }
                }
            }
        }
    }
}